package com.fypg.module.medical.service.internethospital;

import cn.hutool.core.collection.CollectionUtil;
import com.fypg.module.medical.controller.app.internethospital.vo.AppInternetHospitalRespVO;
import com.fypg.module.medical.controller.app.internethospital.vo.AppInternetHospitalVO;
import com.fypg.module.medical.dal.mysql.internethospital.InternetHospitalMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 互联网医院 Service 实现类
 *
 * @author PJY
 */
@Service
@Validated
public class InternetHospitalServiceImpl implements InternetHospitalService {

    private static final Map<String, String> LEVEL_TYPE_MAP;

    static {
        LEVEL_TYPE_MAP = new HashMap<>();
        LEVEL_TYPE_MAP.put("一级", "1");
        LEVEL_TYPE_MAP.put("二级", "2");
        LEVEL_TYPE_MAP.put("三级", "3");
        LEVEL_TYPE_MAP.put("民营", "0");
    }

    @Resource
    private InternetHospitalMapper internetHospitalMapper;

    @Override
    public AppInternetHospitalRespVO<AppInternetHospitalVO> getInternetHospitalList() {
        List<AppInternetHospitalVO> appInternetHospitalVOList = internetHospitalMapper.selectInternetHospitalVOList();

        if (CollectionUtil.isNotEmpty(appInternetHospitalVOList)) {
            appInternetHospitalVOList.forEach(appInternetHospitalVO -> {
                String levelType = appInternetHospitalVO.getLevelType();
                if (levelType == null || levelType.isEmpty()) {
                    appInternetHospitalVO.setLevelType("未定级");
                } else {
                    // 若是没有在 MAP 中，则默认返回原值
                    appInternetHospitalVO.setLevelType(LEVEL_TYPE_MAP.getOrDefault(levelType, levelType));
                }
            });
        }

        return AppInternetHospitalRespVO.<AppInternetHospitalVO>builder()
            .list(appInternetHospitalVOList)
            .total(appInternetHospitalVOList.size())
            .build();
    }
}
